package test;

public class NewTwo20210324 {
    private boolean flag = true;
    private String a;
    private String b;
    private int aIndex;
    private int bIndex;
    public void setA(String a){
        this.a=a;
    }
    public void setB(String b){
        this.b=b;
    }
    public void setAIndex(int aIndex){
        this.aIndex = aIndex;
    }
    public void setBIndex(int bIndex){
        this.bIndex = bIndex;
    }
    public synchronized void printA () {
        while(!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print(a.charAt(aIndex));

        aIndex++;
        flag = false;
        this.notify();
    }

    public synchronized void printB () {
        while(flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print(b.charAt(bIndex));

        bIndex++;
        flag = true;
        this.notify();
    }
}
